﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyObject : OBJBase, ILookToCamera
{
    GameObject obj;
    GameObject rota;
    GameObject anima;
    Pathfinding pathFinding;
    E_Behaviour_State objState;
    float whaitTime = 3;
    bool isDoHide = false;
    int[] stratPath = new int[2];
    int[] endPath = new int[2];
    public GameObject Obj { get => obj; set => obj = value; }
    public GameObject Rota { get => rota; set => rota = value; }
    public GameObject Anima { get => anima; set => anima = value; }
    public E_Behaviour_State ObjState { get => objState; set => objState = value; }

    public EnemyObject()
    {
    }
    public EnemyObject(Vector3 vec, int a)
    {
        ObjType = E_Objects_Type.HostileNpc;
        Obj = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("ObjectRes/Enemy"), new Vector3(vec.x, 0.05f, vec.z), Quaternion.identity);
        rota = Obj.transform.Find("Rotation").gameObject;
        anima = Obj.transform.Find("Rotation/Anima").gameObject;
        LookToCamera();
        pathFinding = new Pathfinding();
        ObjState = E_Behaviour_State.Idle;
        UpdatePath(a);
        MonoMgr.GetInstance().AddUpdateListener(Behaviour);
    }
    public void UpdatePath(int vec0)
    {
        stratPath[0] = (int)Obj.transform.position.z;
        stratPath[1] = (int)Obj.transform.position.x;//重置起点
        endPath[0] = vec0;
        endPath[1] = 23;//重置终点
        pathFinding.UpdatePath(stratPath, endPath);
        pathVec3 = new Vector3(pathFinding.PathList[1][1], 0.05f, pathFinding.PathList[1][0]);
        pathVec2 = new Vector2(pathFinding.PathList[1][1], pathFinding.PathList[1][0]);
        toPathVec = new Vector2(Obj.transform.position.x, Obj.transform.position.z);
        pathIndex = 0;
        ObjState = E_Behaviour_State.Move;
    }
    public void LookToCamera()
    {
        Rota.transform.localEulerAngles = new Vector3(CameraContorller.GetInstance().LookCamera, 0, 0);
    }

    void Behaviour()//对象行为
    {
        switch (ObjState)
        {
            case E_Behaviour_State.Idle:
                break;
            case E_Behaviour_State.Move:
                Move();
                break;
            case E_Behaviour_State.Attack:
                Attack(nowGrid);
                break;
            case E_Behaviour_State.Die:
                break;
        }
    }

    Vector3 pathVec3;
    Vector2 pathVec2;
    Vector2 toPathVec;
    int pathIndex;
    BaseGrid nowGrid;
    void Move()
    {
        whaitTime = 3;
        isDoHide = false;
        if (nowGrid == null)
        {
            nowGrid = new BaseGrid();
        }
        if (Vector2.Distance(toPathVec, pathVec2) > 0.1f)
        {
            Obj.transform.Translate((pathVec3 - Obj.transform.position).normalized * 1f * Time.deltaTime);
            toPathVec.Set(Obj.transform.position.x, Obj.transform.position.z);
        }
        else if (Vector2.Distance(toPathVec, pathVec2) <= 0.1f)
        {
            Obj.transform.position.Set(pathVec2.x, 0.05f, pathVec2.y);
            if (pathIndex + 1 >= pathFinding.PathList.Count)
            {
                ObjState = E_Behaviour_State.Idle;
                GameManager.GetInstance().Game_State = E_Game_State.End;
                Debug.Log("到达终点");
            }
            else
            {
                nowGrid = GetGraph(pathFinding.PathList[pathIndex + 1]);
                if (nowGrid.Lines.Count > 0)
                {
                    ObjState = E_Behaviour_State.Attack;
                }
                else
                {
                    pathIndex = pathIndex + 1;
                    pathVec3.Set(pathFinding.PathList[pathIndex][1], 0.05f, pathFinding.PathList[pathIndex][0]);
                    pathVec2.Set(pathFinding.PathList[pathIndex][1], pathFinding.PathList[pathIndex][0]);
                }
            }

        }
    }
    //获取下一个地图格子的信息
    BaseGrid GetGraph(int[] gridNumber)
    {
        return MapContorller.GetInstance().ArrayGrid[gridNumber[0], gridNumber[1]];
    }

    void Attack(BaseGrid grid)
    {
        HideLine();
        for (int i = 0; i < grid.Lines.Count; i++)
        {
            if (isDoHide)
            {
                MagicController.GetInstance().MagicGraphs[grid.Lines[i][0]].Magiclines[grid.Lines[i][1]].Hide();
            }
        }
        if (GetGraph(pathFinding.PathList[pathIndex + 1]).Lines.Count == 0)
        {
            ObjState = E_Behaviour_State.Move;
        }
    }

    void HideLine()
    {
        whaitTime = whaitTime - 0.5f * Time.deltaTime;
        if (whaitTime < 0)
        {
            isDoHide = true;
        }
    }

    public void Recycle()
    {
        MonoMgr.GetInstance().RemoveUpdateListener(Behaviour);
        GameObject.Destroy(Obj);
    }
}
/// <summary>
/// 闲置|0，移动1|，攻击|2，Die|3
/// </summary>
public enum E_Behaviour_State
{
    Idle = 0,
    Move = 1,
    Attack = 2,
    Die = 3
}